﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using Newtonsoft.Json;

namespace MINIGKP
{
    public class SocketClients
    {
        private static SocketClients instance;
        private SocketClients() { }
        public static SocketClients Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new SocketClients();
                }
                return instance;
            }
        }
        /// <summary>
        /// 创建客户端
        /// </summary>
        public Socket client;
        public void socketclient()
        {
            try
            {
                ///创建客户端
                client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
                ///IP地址
                IPAddress ip = IPAddress.Parse(IPorPortInfo.Instance.CoffeeIP);
                ///端口号
                IPEndPoint endPoint = new IPEndPoint(ip, 8885);
                ///建立与服务器的远程连接
                client.Connect(endPoint);
                ClientList clientList = new ClientList();
                clientList.MsgBol = WuLiaoCoffeeSel.Instance;
                clientList.code = 11;
                client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
                Thread.Sleep(1000);
                ClientList clientList1 = new ClientList();
                clientList1.MsgBol = WuLiaoCoffeeSel.Instance;
                clientList1.code = 10;
                client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList1)));
                ///线程问题
                Thread thread = new Thread(ReciveMsg);
                thread.IsBackground = true;
                thread.Start(client);
            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 客户端接收到服务器发送的消息
        /// </summary>
        /// <param name="o">客户端</param>
        void ReciveMsg(object o)
        {
            Socket client = o as Socket;
            while (true)
            {
                try
                {
                    ///定义客户端接收到的信息大小
                    byte[] arrList = new byte[1024 * 1024];
                    ///接收到的信息大小(所占字节数)
                    int length = client.Receive(arrList);
                    string msg = Encoding.UTF8.GetString(arrList, 0, length);
                    var ClientList = JsonConvert.DeserializeObject<ClientList>(msg);
                    if (ClientList != null)
                    {
                        if (ClientList.code == 10)
                        {
                            MainWindow.OrderNum1 = ClientList.DP.orderNum1;
                            MainWindow.OrderNum2 = ClientList.DP.orderNum2;
                            MainWindow.IsMakeInit1 = ClientList.DP.IsMakeInit1;
                            MainWindow.IsMakeInit2 = ClientList.DP.IsMakeInit2;
                            MainWindow.OrderId1 = ClientList.DP.OrderId1;
                            MainWindow.OrderId2 = ClientList.DP.OrderId2;
                        }
                        else if (ClientList.code == 12)
                        {
                            MainWindow.OrderNum1 = ClientList.DP.orderNum1;
                            MainWindow.OrderNum2 = ClientList.DP.orderNum2;
                            MainWindow.IsMakeInit1 = ClientList.DP.IsMakeInit1;
                            MainWindow.IsMakeInit2 = ClientList.DP.IsMakeInit2;
                            MainWindow.OrderId1 = ClientList.DP.OrderId1;
                            MainWindow.OrderId2 = ClientList.DP.OrderId2;
                        }
                    }
                }
                catch (Exception)
                {
                    ///关闭客户端
                    client.Close();
                }

            }
        }

    }
}
